package cn.iocoder.yudao.module.trade.job.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.trade.config.TradeOrderMailConfig;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.service.order.OrderMailService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 交易订单的自动过期 Job
 *
 * @author 芋道源码
 */
@Slf4j
@Component
public class TradeManualOrderAutoNotifyJob implements JobHandler {

    @Resource
    private TradeOrderQueryService tradeOrderQueryService;

    @Resource
    private TradeOrderMailConfig tradeOrderMailConfig;

    @Resource
    private OrderMailService orderMailService;

    @Override
    @TenantJob
    public String execute(String param) {
        List<TradeOrderDO> orderList = tradeOrderQueryService.getManualOrderList(tradeOrderMailConfig.getManualOrderTimeout());
        if (CollectionUtil.isNotEmpty(orderList)) {
            log.info("超时未处理的订单 {} 个", orderList.size());
            orderMailService.sendManualOrderMail(orderList);
        } else {
            log.info("没有超时未处理的订单");
        }
        return String.format("超时未处理的订单 %s 个", orderList.size());
    }

}
